import 'package:dio/dio.dart';
import 'package:jyofservice/bean/j_c_histroy_entity.dart';
import 'package:jyofservice/util/PhotoGalleryPage.dart';
import 'package:jyofservice/util/webconfig.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class JiChaHistroy extends StatefulWidget {
  String? ybbm;
  JiChaHistroy({this.ybbm});
  @override
  _JiChaHistroyState createState() => _JiChaHistroyState();
}

class _JiChaHistroyState extends State<JiChaHistroy> {

  bool isloading = false;
  List<JCHistroyEntity?> jcList= [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getHistroy();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // backgroundColor: Color(0xFFCDCDCD),
      appBar: AppBar(
        title: Text('稽查记录'),
        centerTitle: true,
      ),
      body: Stack(
        alignment: Alignment(0.5, 0.5),
        children: [
          jcList.length == 0
              ? Container(
              child: Center(
                child: Image.asset("images/nodata.png"),
              )):
              Container(child: ListView.builder(
                itemBuilder: (context, item) {
                  return buildListData(context, item);
                },
                itemCount: jcList.length,
              ))
          ,
          Offstage(
            offstage: isloading,
            child: SpinKitFadingCircle(
              color: Colors.blue,
              size: 50.0,
            ),
          )
        ],
      ),
    );
  }

  Widget buildListData(BuildContext context, int item) {
    return GestureDetector(
      onTap: (){

      },
      child:Container(
        margin: EdgeInsets.fromLTRB(5, 5,5, 5),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(10)),
        ),
        width: ScreenUtil().screenWidth,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              height: ScreenUtil().setHeight(50),
              decoration: BoxDecoration(
                color: Colors.blue,
                borderRadius: BorderRadius.only(topLeft: Radius.circular(10),topRight: Radius.circular(10))),
              child: Center(
                child: Text(
                  jcList[item]!.copyYear??"",style: TextStyle(color: Colors.white,fontSize: ScreenUtil().setSp(40),fontWeight: FontWeight.bold),
                ),
              ),
            ),
            Container(
              padding: EdgeInsets.all(5),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '稽查结果：' + jcList[item]!.jcjg.toString()?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(35),
                        color: Colors.black87),
                  ),
                  Text(
                    '稽查人：' + jcList[item]!.jcr.toString() ?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(30),
                        color: Colors.grey),
                  ),
                  Text(
                    '稽查时间：' + jcList[item]!.jcsj.toString() ?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(30),
                        color: Colors.grey),
                  ),
                  Text(
                    '稽查备注：' + jcList[item]!.jcbz.toString() ?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(30),
                        color: Colors.grey),
                  ),
                  Text(
                    '复核结果：' + jcList[item]!.fhjg.toString()?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(35),
                        color: Colors.black87),
                  ),
                  Text(
                    '复核人：' + jcList[item]!.fhr.toString() ?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(30),
                        color: Colors.grey),
                  ),
                  Text(
                    '复核时间：' + jcList[item]!.fhsj.toString() ?? "",
                    style: TextStyle(
                        fontSize: ScreenUtil().setSp(30),
                        color: Colors.grey),
                  ),
                  SizedBox(
                    height: ScreenUtil().setHeight(10),
                  ),
                  GridView.builder(
                    padding: EdgeInsets.all(5),
                    physics: NeverScrollableScrollPhysics(),
                    shrinkWrap: true,
                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 3,
                      //纵轴间距
                      mainAxisSpacing: 10.0,
                      //横轴间距
                      crossAxisSpacing: 10.0,

                      childAspectRatio: 1,
                    ),
                    itemBuilder: (BuildContext context, int index) {
                      //Widget Function(BuildContext context, int index)
                      List showPhotoList=[];
                      List<String> files = jcList[item]!.files!.split(",");
                      for(int i=0;i<files.length;i++) {
                        var photoStr = {
                          'image':
                          WebConfig().jichaFile + files[i],
                          'id': '$i'
                        };
                        showPhotoList.add(photoStr);
                      }



                      return getFileItem(showPhotoList, index);
                    },
                    itemCount: jcList[item]!.files!.split(",").length,
                  ),
                ],
              ),
            )

          ],
        ),
      ) ,
    );
  }

  Widget getFileItem(List fileList, int index) {
    return GestureDetector(
      onTap: () {
        Navigator.push(context, new MaterialPageRoute(builder: (context) {
          return PhotoGalleryPage(
            index: index,
            photoList: fileList,
          );
        }));
      },
      child: Image.network(
        fileList[index]['image'],
        fit: BoxFit.fill,
      ),
    );
  }

  Future getHistroy() async{
    jcList.clear();
    try {

      Response response = await Dio().get(
        WebConfig().jichaUrl + "method=get_jc_info&ybbm="+widget.ybbm!,);
      print(response.data.toString());

      for (int i = 0; i < response.data.length; i++) {
        JCHistroyEntity? dyListEntity = JCHistroyEntity().fromJson(response.data[i]);
        jcList.add(dyListEntity);
      }
      if(mounted) {
        setState(() {
          isloading = true;
        });
      }

      return response.data;
    } catch (e) {
      print(e.toString());
    }
  }
}
